package com.shop.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.BulkGoodsDetail;
import com.shop.entity.BulkMerchant;
import com.shop.entity.BulkMerchantReturn;
import com.shop.entity.BulkReturnDetail;
import com.shop.mapper.BulkGoodsDetailMapper;
import com.shop.mapper.BulkMerchantMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品批发表
 * gaodo(BulkMerchant)表服务接口
 *
 * @author makejava
 * @since 2020-11-26 20:07:04
 */
@Service
@AllArgsConstructor
public class BulkMerchantService {
    private final BulkMerchantMapper bulkMerchantMapper;
    private final BulkGoodsDetailMapper bulkGoodsDetailMapper;

    /**
     * 获取进货单列表
     *
     * @param pageParam
     * @return
     */
    public PageInfo<BulkMerchant> getBulkMerchantPage(PageParam<BulkMerchant> pageParam,String userId) {
        //设置当前登录用户的id
        pageParam.getModel().setMerchantId(Integer.parseInt(userId));
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            return new PageInfo<BulkMerchant>(bulkMerchantMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        return new PageInfo<BulkMerchant>(bulkMerchantMapper.queryAllByModel(pageParam.getModel()));
    }

    /**
     * 修改配送状态
     * @param bulkMerchant
     * @return
     */
    public Rsp changeStatus(BulkMerchant bulkMerchant) {
        //获取当前状态
        String orderStatus = bulkMerchant.getStatus();
        //设置送达时间
        bulkMerchant.setDeliveryTime(new Date());
        //保存修改结果
        Integer result;
        //如果是1 改为2
        if (orderStatus.equals("2")) {
            bulkMerchant.setStatus("2");
            result = bulkMerchantMapper.changeStatus(bulkMerchant);
        } else {
            bulkMerchant.setStatus("1");
            result = bulkMerchantMapper.changeStatus(bulkMerchant);
        }
        if (result > 0) {
            return Rsp.success(result).msg("修改成功");
        } else {
            return Rsp.fail();
        }
    }
    /**
     * @author clh
     * @description 商家退单订单申请
     * @date  2020/12/2 10:08
     * @param bulkMerchant
     * @return {@link Rsp}
     **/
    public Rsp insertMerchantReturn(BulkMerchant bulkMerchant){
        BulkMerchantReturn bulkMerchantReturn = new BulkMerchantReturn();
        bulkMerchantReturn.setBulkId(bulkMerchant.getId());
        bulkMerchantReturn.setTotal(bulkMerchant.getTotal());
        bulkMerchantReturn.setSubTitle(bulkMerchant.getSubTitle());
        bulkMerchantReturn.setMerchantId(bulkMerchant.getMerchantId());
        bulkMerchantReturn.setCount(bulkMerchant.getCount());
        bulkMerchantReturn.setTotal(bulkMerchant.getTotal());
        bulkMerchantReturn.setMerchantName(bulkMerchant.getMerchantName());
        bulkMerchantReturn.setMerchantPhone(bulkMerchant.getMerchantPhone());
        bulkMerchantReturn.setSupplyId(bulkMerchant.getSupplyId());
        bulkMerchantReturn.setSupplyName(bulkMerchant.getSupplyName());
        bulkMerchantMapper.insertMerchantReturn(bulkMerchantReturn);
        bulkMerchantMapper.updateMerchantReturnStatus("3",bulkMerchant.getId());
        List<BulkGoodsDetail> detailList = bulkGoodsDetailMapper.getAll(bulkMerchant.getId());
        List<BulkReturnDetail> list = new ArrayList<>();
        for (BulkGoodsDetail bulkGoodsDetail:detailList) {
            BulkReturnDetail bulkReturnDetail = new BulkReturnDetail();
            bulkReturnDetail.setReturnId(bulkMerchantReturn.getId());
            bulkReturnDetail.setGoodsId(bulkGoodsDetail.getGoodsId());
            bulkReturnDetail.setGoodsName(bulkGoodsDetail.getGoodsName());
            bulkReturnDetail.setGoodsCount(bulkGoodsDetail.getGoodsCount());
            bulkReturnDetail.setGoodsPrice(bulkGoodsDetail.getGoodsPrice());
            bulkReturnDetail.setGoodsTotal(bulkGoodsDetail.getGoodsTotal());
            bulkReturnDetail.setGoodsImg(bulkGoodsDetail.getGoodsImage());
            list.add(bulkReturnDetail);
        }
        bulkMerchantMapper.insertList(list);
        return Rsp.success(true).msg("退货申请成功");
    }
    /**
     * @author clh
     * @description 单个退单操作
     * @date  2020/12/2 11:00
     * @param bulkGoodsDetail
     * @return {@link Rsp}
     **/
    public Rsp merchantReturnOne(BulkGoodsDetail bulkGoodsDetail){
        BulkMerchant bulkMerchant = bulkMerchantMapper.getById(bulkGoodsDetail.getBulkId());
        BulkMerchantReturn bulkMerchantReturn = new BulkMerchantReturn();
        bulkMerchantReturn.setBulkId(bulkMerchant.getId());
        bulkMerchantReturn.setTotal(bulkMerchant.getTotal());
        bulkMerchantReturn.setSubTitle(bulkMerchant.getSubTitle());
        bulkMerchantReturn.setMerchantId(bulkMerchant.getMerchantId());
        bulkMerchantReturn.setCount(bulkMerchant.getCount());
        bulkMerchantReturn.setTotal(bulkMerchant.getTotal());
        bulkMerchantReturn.setMerchantName(bulkMerchant.getMerchantName());
        bulkMerchantReturn.setMerchantPhone(bulkMerchant.getMerchantPhone());
        bulkMerchantReturn.setSupplyId(bulkMerchant.getSupplyId());
        bulkMerchantReturn.setSupplyName(bulkMerchant.getSupplyName());
        bulkMerchantMapper.insertMerchantReturn(bulkMerchantReturn);
        bulkMerchantMapper.updateMerchantReturnStatus("3",bulkMerchant.getId());

        BulkReturnDetail bulkReturnDetail = new BulkReturnDetail();
        bulkReturnDetail.setReturnId(bulkMerchantReturn.getId());
        bulkReturnDetail.setGoodsId(bulkGoodsDetail.getGoodsId());
        bulkReturnDetail.setGoodsName(bulkGoodsDetail.getGoodsName());
        bulkReturnDetail.setGoodsCount(bulkGoodsDetail.getGoodsCount());
        bulkReturnDetail.setGoodsPrice(bulkGoodsDetail.getGoodsPrice());
        bulkReturnDetail.setGoodsTotal(bulkGoodsDetail.getGoodsTotal());
        bulkReturnDetail.setGoodsImg(bulkGoodsDetail.getGoodsImage());
        bulkMerchantMapper.insertReturnDetail(bulkReturnDetail);
        return Rsp.success(true).msg("退货申请成功");
    }
}
